<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use Hash;
use DB;
use App\Http\Requests;
use App\Http\Requests\AdminfriendRequest;
use App\Http\Requests\AdminFriendUpdateRequest;
use App\Http\Controllers\Controller;

class FriendController extends Controller
{
    /*
        友链添加页面显示
    */
    public function getAdd()
    {
        return view('admin.friend.add');
    }

    /*
        执行友链添加
    */
    public function postInsert(AdminfriendRequest $request)
    {
        //判断友链地址是否为空
        if(empty($request->input('friend_url'))){
            return back()->with('error','友链地址不能为空');
        }
        //接收部分数据
        $data = $request->only(['friend_name','friend_url']);
        //随机文件名
        $name = md5(time()+rand(1,9999999));
        //获取后缀名
        $su = $request->file('friend_logo')->getClientOriginalExtension();
        //移动文件
        $request->file('friend_logo')->move('./uploads/',$name.'.'.$su);
        //将文件路径压入到数组中
        $data['friend_logo'] = '/uploads/'.$name.'.'.$su;
        //插入到数据库中
        $res = DB::table('friend')->insert($data);
        if($res){
            return redirect('/admin/friend/index')->with('success','友链添加成功');
        }else{
            return back()->with('error','友链添加失败');
        }
    }

    /*
        友链列表
    */
    public function getIndex(Request $request)
    {
        //每页显示几条
        $num = $request->input('num',4);

        if(!empty($request->input('keywords'))){
            $friend = Db::table('friend')->where('friend_name','like','%'.$request->input('keywords').'%')->paginate($num);
        }else{
            //查询所有的友链
            $friend = DB::table('friend')->paginate($num);
        }

        //解析模板
        return view('admin.friend.index',['friend'=>$friend,'request'=>$request->all()]);
    }

    /*
        显示修改友链页面
    */
    public function getEdit($id)
    {
        //根据id查询友链
        $friend = DB::table('friend')->where('id',$id)->first();

        //解析模板
        return view('admin.friend.edit',['friend'=>$friend]);
    }

    /*
        执行修改
    */
    public function postUpdate(AdminFriendUpdateRequest $request)
    {
        //接收id
        $id = $request->input('id');
        //判断是否有文件上传
        if($request->hasFile('friend_logo')){
            //随机文件名
            $name = md5(time()+rand(1,9999999));
            //获取扩展名
            $su = $request->file('friend_logo')->getClientOriginalExtension();
            //移动文件
            $request->file('friend_logo')->move('./uploads/',$name.'.'.$su);
            //获取部分参数
            $data = $request->only(['friend_name','friend_url']);
            $data['friend_logo'] = '/uploads/'.$name.'.'.$su;
        }else{
            //接收修改之前的值
            $a = $request->only(['refriend_name','refriend_url']);
            //把$a的值取出来组成一个新数组
            $aa = array_values($a);
            //接收修改之后的值
            $b = $request->only(['friend_name','friend_url']);
            //把$b的值取出来组成一个新数组
            $bb = array_values($b);
            //判断用户是否修改
            if($aa == $bb){
                return redirect('/admin/friend/index')->with('success','修改成功');
            }

            //获取部分参数
            $data = $b;
        }
        //执行修改
        $res = DB::table('friend')->where('id',$id)->update($data);
        //判断
        if($res){
            return redirect('/admin/friend/index')->with('success','修改成功');
        }else{
            return back()->with('error','修改失败');
        }
    }

    /*
        删除友链
    */
    public function postDelete(Request $request)
    {
        //接收id
        $id = $request->input('id');
        
        //执行删除
        $res = DB::table('friend')->where('id',$id)->delete();
        //判断
        if($res){
            return 2;
        }else{
            return 1;
        }
    }
}
